package com.ruoyi.file.service;

import org.apache.tomcat.util.http.fileupload.FileItem;
import org.apache.tomcat.util.http.fileupload.FileItemFactory;

import java.io.File;

public class MyFileItemFactory implements FileItemFactory {

    private File storeFile;
    private boolean usable = true;

    public MyFileItemFactory(File storeFile) {
        this.storeFile = storeFile;
    }

    @Override
    public FileItem createItem(final String fieldName, final String contentType,
                               final boolean isFormField, final String fileName) {
        if (!usable) {
            throw new RuntimeException("仅支持单文件上传");
        }
        usable = false;
        MyFileItem myFileItem = new MyFileItem(storeFile);
        myFileItem.setName(fileName);
        return myFileItem;
    }
}
